<?php
/**
 * Description of Relation
 *
 * @author Admin
 */

class Main_Model_Relation extends WOW_Model_Acl_Abstract
{

    public function getRelationById($id)
    {
        $id = (int) $id;
        return $this->getResource('Relation')->getRelationById($id);
    }

    public function getRelationByIdent($ident)
    {
        return $this->getResource('Relation')->getRelationByIdent($ident);
    }
    

    protected function _saveRelation(Zend_Form $form, array $info, $defaults=array())
    {
        if (!$form->isValid($info))
        {
            return false;
        }

        $data = $form->getValues();
        foreach ($defaults as $col => $value)
        {
            $data[$col] = $value;
        }
        if($data['id'] !='')
        {
            $data['RelationName'] = ucwords($data['RelationName']);
            $data['updated_at'] = date('Y-m-d H:i:s');
            $ident = $data['RelationName'];
            $data['relation_ident']   = str_replace(' ', '-', strtolower($ident));
        }
        else
        {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $ident = $data['RelationName'];
            $data['relation_ident']   = str_replace(' ', '-', strtolower($ident));
        }

        $relation = ($data['id'] !='') ? $this->getResource('Relation')->getRelationById($data['id']) : null;

        return $this->getResource('Relation')->saveRow($data, $relation);

    }

    public function saveRelation($post)
    {
        $form = $this->getForm('RelationRelationform');
        return $this->_saveRelation($form, $post);
    }

     /**
     * Delete a relation
     *
     * @param int|Main_Resource_Relation_Item_Interface $relation
     * @return boolean
     */
    public function deleteRelation($relation)
    {
        if ($relation instanceof Main_Resource_Relation_Item_Interface) {
            $relationid = (int) $relation->id;
        } else {
            $relationid = (int) $relation;
        }

        $relation = $this->getRelationById($relationid);

        if (null !== $relation) {
            $relation->delete();
            return true;
        }
        else
             throw new WOW_Exception('Unknown relation');

        return false;
    }

    public function getAllRelations()
    {
        return $this->getResource('Relation')->getAllRelations();
    }

    public function getResourceId()
    {
        return 'Relation';
    }

    #@ TODO:: needs to be worked on ACL later
    public function setAcl(WOW_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId()))
        {
            $acl->add($this)->allow('Normal', $this, array('register'))
            ->allow('Moderator', $this, array('saveUser'))
                    ->allow('Admin', $this);
        }
        $this->_acl = $acl;
        return $this;
    }

    public function getAcl()
    {
        if (null === $this->_acl) {
            $this->setAcl(new Main_Model_Acl_Main());
        }
        return $this->_acl;
    }

}
?>
